import numpy as np
from scipy.interpolate import interp1d
import pandas as pd
from time2vec import SineActivation
import torch


def t2v(data):
    sinact = SineActivation(in_features=len(data), out_features=len(data))
    result = sinact(data)

    return result

def clean(data_path):
    data = pd.read_csv(data_path)
    data['DCOILWTICO'] = pd.to_numeric(data['DCOILWTICO'], errors='coerce')
    date_index = pd.Series(range(1, len(data['DCOILWTICO']) + 1), name='Date_index')
    # 找到NaN或0的位置
    nan_or_zero_indices = np.where((np.isnan(data['DCOILWTICO'])) | (data['DCOILWTICO'] == 0))[0]

    # 找到非NaN且非0的位置
    valid_indices = np.where(~np.isnan(data['DCOILWTICO']) & (data['DCOILWTICO'] != 0))[0]
    valid_values = data['DCOILWTICO'][valid_indices]

    # 创建插值函数，使用原始数据的索引作为输入
    interp_func = interp1d(valid_indices, valid_values, kind='quadratic', fill_value="extrapolate")

    # 使用插值函数填充NaN值或0值
    filled_data = data['DCOILWTICO'].copy()
    filled_data[nan_or_zero_indices] = interp_func(nan_or_zero_indices)

    # 删除价格小于0的行
    cleaned_data = pd.concat([date_index, filled_data], axis=1)
    cleaned_data = cleaned_data[cleaned_data['DCOILWTICO']>0]

    #将时间转换为向量
    t_time = torch.tensor(cleaned_data['Date_index'].values).float()
    temp = t2v(t_time)
    cleaned_data['Date_index'] = pd.Series(temp.detach().numpy())

    #返回DataFrame/Series
    return cleaned_data


if __name__=="__main__":
    data_path = "/Users/hern/Downloads/DCOILWTICO.csv"
    a = clean(data_path)

